/*
Copyright (c) 2006 - 2008  Universitaet Osnabrueck, virtUOS
Authors: Markus Ketterl, Johannes Emden, Nils Schuhmacher, Andreas Rickling

This file is part of virtPresenter.

virtPresenter is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

virtPresenter is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with virtPresenter.  If not, see <http://www.gnu.org/licenses/>.
*/

package virtPresenter.commands
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.IResponder;
	
	import virtPresenter.business.*;
	import virtPresenter.event.LoadLogXMLEvent;
	import virtPresenter.model.TimeStep;
	import virtPresenter.model.virtPresenterLocator;	
	
	/**
	 * Commands handles a LoadLogXMLEvent and parses the logXML.xml and search.xml.	
	 */	
	public class LoadLogXMLCommand implements ICommand, IResponder
	{	
		protected var model:virtPresenterLocator=virtPresenterLocator.getInstance();
		protected var logArray: ArrayCollection;
		protected var logXMLtmp:XML;
		
		/**
		 * Execute method is invoked by the LoadLogXMLEvent.
		 * Calls its delegate to recieve the result of the mouse.xml file.
		 * 
		 * @parameters event LoadLogXMLEvent
		 * 
		 * @see virtPresenter.event.LoadLogXMLEvent
		 */
		public function execute(event:CairngormEvent):void
		{
			var folder:String = LoadLogXMLEvent(event).folder;				
			var delegate:LogXMLDelegate = new LogXMLDelegate(this);
			delegate.getLogXML(folder);		
		}
		
		/**
		 * Result method is called by the SearchXMLDelegate and stores the results
		 * of search.xml as an ArrayCollection in the model. Parses the search.xml and
		 * logXML.xml. Finally it invokes connecting the video by calling the <code>files_both_loaded
		 * method</code> of the XMLParser.		 
		 *  
		 * @parameters data Data Object that contains the results of the request to the search.xml
		 * 
		 * @see virtPresenter.business.SearchXMLDelegate
		 */
		public function result(data:Object):void
		{	
			model.logCollection = ArrayCollection(data.result.Presentation.Event);
			trace("LoadLogXMLCommand. LogXML.xml loaded! ");
			logXMLtmp=new XML(data.result);
			parseLogXML();
			parseSearchXML();
			parseSearchXMLAnimationStep();
			model.xmlParser.files_both_loaded();
			model.xmlReceived=true;		
		}
		
		/**
		 * Fault method is invoked by SearchXMLDelegate if an error occured
		 * loading the search.xml file.
		 * 
		 * @parameters info Info object that contains information about the fault
		 * 
		 * @see virtPresenter.business.SearchXMLDelegate  
		 */
		public function fault(info:Object):void
		{
			trace("LoadLogXMLCommand. Fault in loading logXML.xml:"+info.toString());
		}
		
		private function parseLogXML():void
		{
			var xmlTimeSteps:XML = logXMLtmp;
			model.xmlParser.timeSteps = new Array(); // Erstelle ein leeres Array
			
			for each (var item:Object in model.logCollection)
			{
				var tmp:TimeStep = new TimeStep();
				tmp.setTime(int(item.Time.toString()));
				tmp.setStep(int(item.Step.toString()));
				model.xmlParser.timeSteps.push(tmp);	
			}
			
			/* WICHTIG: Die Folien müssen nach ihrem Zeitstempel sortiert sein,
			 *          damit nach ihnen gesucht werden kann.
			 */
			model.xmlParser.timeSteps.sort(model.xmlParser.sortOnTime);			
		}
		
		private function parseSearchXML():void
		{							
			for each (var item:Object in model.searchCollection)
			{
				var id:int=int(item.ID.toString());
				var startStep:int=int(item.startStep.toString());
				var endStep:int=int(item.endStep.toString());
				var title:String = item.title.toString();
				var textOnSlide:String;
				if(item.textOnSlide!=null)textOnSlide = item.textOnSlide.toString();
				else textOnSlide="";
			
				for(var i:int=0; i<model.xmlParser.timeSteps.length;i++)
				{
					var tmp:TimeStep = model.xmlParser.timeSteps[i];
					if(tmp.getStep()>=startStep && tmp.getStep()<=endStep)
					{
						tmp.setID(id);	
						tmp.setTitle(title);					
						//Muss +1 nicht weg, denn es gibt auch Folien, die keine Animationsschritte haben	
						//tmp.setAnimationStep(tmp.getStep()-startStep+1);
						tmp.setAnimationStep(tmp.getStep()-startStep);
						
						//only the first timestep of a slide recieves the text
						if(tmp.getStep()==startStep)
						{
							tmp.setTextOnSlide(textOnSlide);
						}			
					}
				}
			}
									
			model.xmlParser.files_both_loaded();
			//trace("LoadLogXMLCommand. TimeSteps: "+model.xmlParser.timeSteps.toString());
		}
		
		/**
		 * Sets text for every animationstep.
		 * */
		private function parseSearchXMLAnimationStep():void
		{	
			/* trace("Bin drin!!!:"+model.searchXML.slide.title);		
			for each (var animationSequence:XML in model.searchXML.animations.animationSequence)
			{		trace("Juhu");	
				for each (var animation:XML in animationSequence.animation)
				{
					var step:int = int(animationSequence.@step.toString());
					var text:String = animation.toString();
					if(text!=null)
						addTextToTimeStep(step,text);
					trace(step+": "+text);
				}
			
			} */
			for (var i:int=0;i<model.searchCollection.length;i++)
			{
				if(model.searchCollection[i].animations!=null)
				{
					if ((model.searchCollection[i].animations.animationSequence) is ArrayCollection)
					{
						var animationSequence:ArrayCollection=model.searchCollection[i].animations.animationSequence;
								
							for (var j:int=0;j<animationSequence.length;j++)
							{
								var step:int = int(animationSequence[j].step.toString());
								if (animationSequence[j].animation!=null)
								{
									var text:String = animationSequence[j].animation.toString();
									addTextToTimeStep(step,text);
								}
								//trace(LoadLogXMLCommand. step+": "+text);
							}
					}
					
					else
					{
						if ((model.searchCollection[i].animations.animationSequence.animation)is ArrayCollection)
						{
							var animation:ArrayCollection=model.searchCollection[i].animations.animationSequence.animation;
							
							for (var k:int=0;k<animation.length;k++)
							{
								var step2:int = int(model.searchCollection[i].animations.animationSequence.step2.toString());
								if(animation[k]!=null)
								{
									var text2:String = animation[k].toString();
									addTextToTimeStep(step2,text2);
								}
								//trace(LoadLogXMLCommand. step+": "+text);
							}
						}					
					}
					
				
				}
			}
			
		}
		
		//Not very performant
		private function addTextToTimeStep(timestep:int, text:String):void
		{
			for(var i:int=0; i<model.xmlParser.timeSteps.length;i++)
			{
				var tmp:TimeStep = model.xmlParser.timeSteps[i];
				if(timestep==tmp.getStep())
				{

					tmp.setTextOnSlide(tmp.getTextOnSlide()+" "+text);
				}	
			}	
		}
		
	}
}